<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Model\Cate;
use App\Http\Model\Goods;
use App\Http\Model\Home_user;
use App\Http\Model\Type;
use App\Http\Model\UserInfo;
use DB;
class GoodsListController extends Controller
{
    /**
     * 公共方法
     */
    public function pub2(){
        $link = \DB::table('sad_link')->orderBy('order','asc')->get();
        return $link;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index($id)
    {
        //
        $data = Goods::find($id);
        // dd($data);
        //将商品列表中的uid替换为卖家姓名
        @$info1 = Goods::find($id)->home_user->username;
        $data->uid = $info1;

        //将商品列表中的tid替换为板块名称
        @$info2 = Goods::find($id)->type->name;
        $data->tid = $info2;

        //将商品列表中的condition值更改输出
        $c = '';
            switch ($data->condition) {
                case '0':
                    $c = '九九新';
                    break;
                case '1':
                
                    $c = '九成新';
                    break;
                case '2':
                    $c = '八成新';
                    break;
                case '3':
                    $c = '七成新';
                    break;
                
                case '4':
                    $c = '垃圾成色';
                    break;       
            }
        $data->condition = $c;

            // dd($data);
        $link = $this->pub2();



        //1 获取前台分类遍历的数据

       
         $comment = DB::table('sad_comment')
                    ->join('sad_home_user','sad_comment.uid','=','sad_home_user.id')
                    ->join('sad_userinfo','sad_comment.uid','=','sad_userinfo.id')
                    ->get();


        // dd($comment);


        return view('home.goods.index',['data'=>$data,'link'=>$link,'comment'=>$comment]);

    }

    public function buy($gid)
    {   
        $data = Goods::find($gid);
        // dd($gid);
        //将商品列表中的condition值更改输出
        $c = '';
            switch ($data->condition) {
                case '0':
                    $c = '九九新';
                    break;
                case '1':
                
                    $c = '九成新';
                    break;
                case '2':
                    $c = '八成新';
                    break;
                case '3':
                    $c = '七成新';
                    break;
                
                case '4':
                    $c = '垃圾成色';
                    break;       
            }
        $data->condition = $c;
        $uid = session('huser')->id;
        $userinfo = UserInfo::where('uid',$uid)->first();
        $user = session('huser');
        // dd($user);
        $link = $this->pub2();
        return view('home.goods.buy',['link'=>$link,'data'=>$data,'user'=>$user,'userinfo'=>$userinfo]);
    }

    public function pay(Request $request)
    {   


        // dd($request);
        $rid = $request->rid;
        $gid = $request->gid;
        $uid = $request->uid;
        $name = $request->name;
        $tel = $request->tel;
        $address = $request->address;
        $cost = $request->cost;
        $ordertime = date('Y-m-d H:i:s',time());
        $code = date('YmdHis',time()).$rid.$uid.$gid;
        $arr = ['rid'=>$rid,'gid'=>$gid,'uid'=>$uid,'name'=>$name,'tel'=>$tel,'address'=>$address,'cost'=>$cost,'code'=>$code,'ordertime'=>$ordertime,'num'=>1];
        // session(['order'=>$arr]);
        session('order',$arr);
        $data = DB::table('sad_order')->insert($arr);
        return $code;
    }

     public function payaa($code)
     {   
        $data = DB::table('sad_order')->where('code',$code)->first();
        
        $data2 = DB::table('sad_goods')->where('id',$data['gid'])->first();
        $data2['status'] = '待发货';
        DB::table('sad_goods')->where('id',$data['gid'])->update($data2);
        
        $link = $this -> pub2();
        return view('home.pay',['link'=>$link,'data'=>$data,'data2'=>$data2]);
     }

     public function info($id)
     {
        
        $orders=DB::table('sad_order')->where('id',$id)->first();
        $code=$orders['code'];
        $link = $this -> pub2();
        return view('home.goods.info',['link'=>$link,'code'=>$code]);
     }


     /**
      * 商品品论列表
      */
     public function reply(Request $request)
     {      

        $uid = session('huser')->id;

        $data = $request->all();

        // dd($data);

        $gid = $data['gid'];

        $content = $data['reply'];

        $res = DB::table('sad_comment')->insert(['uid'=>$uid,'gid'=>$gid,'content'=>$content]);

        if($res)
        {

            return back()->with('success','评论成功');
        }else
        {

            return back()->with('error','评论失败');
        }


     }
    
}
